package com.duo.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.duo.constant.StatusConstant;
import com.duo.dto.SetmealPageQueryDTO;
import com.duo.entity.Setmeal;
import com.duo.entity.SetmealDish;
import com.duo.mapper.SetmealDishMapper;
import com.duo.service.SetmealService;
import com.duo.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController("userSetmealController")
@RequestMapping("/user/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 分页查询套餐
     * @return
     */
    @PostMapping("/page")
    public Object select(@RequestBody SetmealPageQueryDTO setmealPageQueryDTO) {
        return setmealService.pageList(setmealPageQueryDTO);
    }

    /**
     * 根据分类id查询全部套餐
     */
    @GetMapping("/list/{categoryId}")
    public Object list(@PathVariable("categoryId") Long categoryId) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, categoryId);
        queryWrapper.eq(Setmeal::getStatus, StatusConstant.ENABLE);
        List<Setmeal> list = setmealService.list(queryWrapper);
        List<SetmealVO> result = new ArrayList<>();
        list.forEach(setmeal -> {
            SetmealVO setmealVO = new SetmealVO();
            setmealVO.setSetmealId(setmeal.getId());
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
            setmealVO.setSetmealDishes(setmealDishes);
            result.add(setmealVO);
        });
        return result;
    }




}
